package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.DataDictionary;
import cn.wolfcode.crm.query.ObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IDataDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("dataDictionary")
public class DataDictionaryController {
    @Autowired
    private IDataDictionaryService dataDictionaryService;

    //用于跳转到数据字典页面
    @RequiresPermissions("dataDictionary:index")
    @PermissionName("数据字典主页")
    @RequestMapping("index")
    public String index() {
        return "dataDictionary/list";
    }


    @RequestMapping("list")
    @RequiresPermissions("dataDictionary:list")
    @PermissionName("数据字典列表")
    @ResponseBody
    public PageResult list(@ModelAttribute("qo") ObjectQuery qo) {
        //返回json格式的查询结果对象
        PageResult<DataDictionary> list = dataDictionaryService.query(qo);
        return list;
    }

    @RequiresPermissions("dataDictionary:saveOrUpdate")
    @PermissionName("数据字典创建更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Model model, DataDictionary dataDictionary) {
        JsonResult json = new JsonResult();
        try {
            if (dataDictionary.getId() != null) {
                dataDictionaryService.update(dataDictionary);
            } else {
                dataDictionaryService.save(dataDictionary);

            }
        } catch (Exception e) {
            e.printStackTrace();
            json.markErrorMsg("更新操作失败!");
        }
        return json;
    }


    //数据字典删除
    @RequiresPermissions("dataDictionary:delete")
    @PermissionName("数据字典删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                dataDictionaryService.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("删除数据字典失败");
        }
        return jsonResult;
    }

    //根据传入的sn从数据字典中查询相关数据
    @RequestMapping("selectDataBySn")
    @ResponseBody
    public List<DataDictionary> selectDataBySn(String sn) {
        List<DataDictionary> list = dataDictionaryService.selectDataBySn(sn);
        return list;
    }

    /**
     * rui:根据传入的id从数据字典中查询对应的数据
     *
     * @param id 页面传入过来的数据字典的id
     * @return 返回数据字典数据
     */
    @RequestMapping("getDataDictionaryById")
    @ResponseBody
    public DataDictionary getDataDictionaryById(Long id) {
        DataDictionary data = dataDictionaryService.get(id);
        return data;
    }
    /**
     * rui:根据传入的id从数据字典中查询对应支付方式
     *
     * @return 返回数据字典数据
     */
    @RequestMapping("queryPayWayById")
    @ResponseBody
    public List<DataDictionary> queryPayWayById() {
        System.out.println(1);
        return dataDictionaryService.queryPayWayById();
    }

}
